import {action, computed, observable} from 'mobx';
import {message} from 'antd';
import {
    programAccessItemApi,
    programItemDetailApi,
    programItemListApi,
    programItemOperationApi
} from "../../apis/performance/programItemApi";
import {WeaTableNew} from "comsMobx";
import {WeaLocaleProvider} from 'ecCom';
import {startWith, endWith} from '../../util/Util'
const getLabel = WeaLocaleProvider.getLabel;

/**
 * 绩效-方案指标项
 * jiansong 2019/6/11
 */
const {TableStore} = WeaTableNew;
export default class ProgramItemStore {

    @observable loading = true; //加载中
    @observable tableStore = new TableStore(); // new table
    @observable accessItemList = []; //有权限访问的考核指标列表
    //@observable showItem = false; //展示内容
    @observable showStatus = false; //展示状态还是编辑状态
    @observable form = { //考核项编辑form
        id: '',
        name: '', //指标模板名称
        description: '', //指标模板说明
        assessDetailData: [], //考核指标项数组
    };
    currentorder = 0;

    @action("清空表单")
    clearForm = () => {
        this.form = {
            id: '',
            name: '',
            description: '',
            assessDetailData: [],
        };
    }

    @action("搜索条件改变")
    onFormChange = (params) => {
        for (let key in params) {
            this.form[key] = params[key];
        }
    }

    @action("更改考核项显示或隐藏")
    changeShowItem = (showItem) => {
        if (!showItem) {
            this.clearForm();
        }
        this.showItem = showItem;
    }

    //获取详情
    getItemDetail = (id) => {
        this.getProgramAccessItemList();
        programItemDetailApi({id: id}).then(action(res => {
            if (res.api_status) {
                this.form.id = res.id;
                this.form.name = res.name;
                this.showStatus = res.canedit;
                this.form.description = res.description;
                this.form.assessDetailData = res.item;
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
        }));
    }

    @action("获取考核指标列表")
    getProgramAccessItemList = () => {
        programAccessItemApi().then(action(res => {
            if (res.api_status) {
                this.accessItemList = res.accessItemList;
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
        }));
    }

    @action("获取考核指标模板列表")
    getProgramItemList = (params) => {
        this.loading = true;
        programItemListApi(params).then(action(res => {
            if (res.api_status) {
                this.tableStore.getDatas(res.datas, 1);
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
            this.loading = false;
        }));
    }

    @action("操作数据")
    operation = (onClick) => {
        this.loading = true;
        this.form.assessDetailData = JSON.stringify(this.form.assessDetailData);
        programItemOperationApi(this.form).then((res) => {
            if (res.api_status) {
                if (onClick) {
                    onClick(res.id, this.form.name,1);
                }
                message.success(getLabel('30700', '操作成功'));
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
            this.loading = false;
        })
    }
    @action("合并评分人数据")
    getNewAccessDate = (detailList) =>{
        let datalist = [];
        let scoreattr = 0;
        let newexeorder = 0;
        detailList && detailList.length>0 && detailList.map((element,idx)=>{
            if((element.scoreattr==scoreattr && element.newexeorder==newexeorder) || (element.scoreattr==scoreattr && element.scoreattr==0)){
                if(datalist && datalist.length>0){
                    datalist[datalist.length-1].showid = datalist[datalist.length-1].showid+","+element.checkuserid;
                }else{
                    datalist.push({showid:element.checkuserid,showattr:element.scoreattr,newexeorder:element.newexeorder});
                }
            }else{
                datalist.push({showid:element.checkuserid,showattr:element.scoreattr,newexeorder:element.newexeorder});
                scoreattr = element.scoreattr;
                newexeorder = element.newexeorder;
            }
            this.currentorder = element.newexeorder;
        });
        datalist && datalist.length>0 && datalist.map((element,idx)=>{
            if(!startWith(element.showid)){
                element.showid = ","+element.showid;
            }
            if(!endWith(element.showid)){
                element.showid = element.showid+",";
            }
        });
        return datalist;
    }
}